//
//  UserEntity.m
//  SXH
//
//  Created by 房 国生 on 15/7/16.
//  Copyright (c) 2015年 lyss. All rights reserved.
//

#import "UserEntity.h"

@implementation UserEntity
@synthesize login;

static UserEntity * _sharedInstance = nil;

+ (UserEntity*)sharedInstance
{
    static dispatch_once_t p = 0;
    dispatch_once(&p, ^{
        _sharedInstance = [[UserEntity alloc] init];
    });
    return _sharedInstance;
}

- (instancetype)initWithAttributes:(NSDictionary *)attributes
{
    self = [super init];
    if (!self) {
        return nil;
    }
    
    NSNumber *user_id = [attributes valueForKeyPath:@"user_id"];
    self.userId = [NSString stringWithFormat:@"%d", [user_id intValue]];
    self.userAlbum = [NSString stringWithFormat:@"%@/%@", IMAGE_URL, [attributes valueForKeyPath:@"user_album"]];
    self.nickName= [attributes valueForKeyPath:@"nickname"];
    self.signature= [attributes valueForKeyPath:@"signature"];
    self.birthday= [attributes valueForKeyPath:@"birthday"];
    
    NSNumber *attention_count = [attributes valueForKeyPath:@"attention_count"];
    self.attentionCount = [NSString stringWithFormat:@"%d", [attention_count intValue]];
    
    NSNumber *fans_count = [attributes valueForKeyPath:@"fans_count"];
    self.fansCount = [NSString stringWithFormat:@"%d", [fans_count intValue]];
    
    NSNumber *good_count = [attributes valueForKeyPath:@"good_count"];
    self.goodCount = [NSString stringWithFormat:@"%d", [good_count intValue]];
    
    self.sex= [attributes valueForKeyPath:@"sex"];
    self.cityName= [attributes valueForKeyPath:@"city_name"];
    
    NSNumber *is_vip = [attributes valueForKeyPath:@"is_vip"];
    self.isVip= [NSString stringWithFormat:@"%d", [is_vip intValue]];
    NSNumber *flag = [attributes valueForKeyPath:@"flag"];
    self.flag= [NSString stringWithFormat:@"%d", [flag intValue]];
    
    self.attention_flag = [attributes valueForKeyPath:@"attention_flag"];
   
    NSNumber *followed = [attributes valueForKeyPath:@"followed"];
    self.followed = [NSString stringWithFormat:@"%d", [followed intValue]];

    return self;
}

- (void) deepCopy:(UserEntity *)sender
{
    _sharedInstance.userId = [sender.userId mutableCopy];
    _sharedInstance.userAlbum = [sender.userAlbum mutableCopy];
    _sharedInstance.nickName = [sender.nickName mutableCopy];
    _sharedInstance.signature = [sender.signature mutableCopy];
    _sharedInstance.birthday = [sender.birthday mutableCopy];
    _sharedInstance.attentionCount = [sender.attentionCount mutableCopy];
    _sharedInstance.fansCount = [sender.fansCount mutableCopy];
    _sharedInstance.goodCount = [sender.goodCount mutableCopy];
    _sharedInstance.sex = [sender.sex mutableCopy];
    _sharedInstance.cityName = [sender.cityName mutableCopy];
    _sharedInstance.login = sender.login;
    _sharedInstance.attention_flag = sender.attention_flag;
    _sharedInstance.isCircle = sender.isCircle;
    _sharedInstance.session_id = sender.session_id;
}

- (void)encodeWithCoder:(NSCoder *)encoder{
    [encoder encodeObject:self.userId forKey:@"userId"];
    [encoder encodeObject:self.userAlbum forKey:@"userAlbum"];
    [encoder encodeObject:self.nickName forKey:@"nickName"];
    [encoder encodeObject:self.signature forKey:@"signature"];
    [encoder encodeObject:self.birthday forKey:@"birthday"];
    [encoder encodeObject:self.attentionCount forKey:@"attentionCount"];
    [encoder encodeObject:self.fansCount forKey:@"fansCount"];
    [encoder encodeObject:self.goodCount forKey:@"goodCount"];
    [encoder encodeObject:self.sex forKey:@"sex"];
    [encoder encodeBool:self.login forKey:@"login"];
    [encoder encodeObject:self.cityName forKey:@"cityName"];
    [encoder encodeBool:self.isCircle forKey:@"isCircle"];
    [encoder encodeObject:self.session_id forKey:@"session_id"];

}

- (id)initWithCoder:(NSCoder *)decoder{
    if(self = [super init]) {
        self.userId = [decoder decodeObjectForKey:@"userId"];
        self.userAlbum = [decoder decodeObjectForKey:@"userAlbum"];
        self.nickName = [decoder decodeObjectForKey:@"nickName"];
        self.signature = [decoder decodeObjectForKey:@"signature"];
        self.birthday = [decoder decodeObjectForKey:@"birthday"];
        self.attentionCount = [decoder decodeObjectForKey:@"attentionCount"];
        self.fansCount = [decoder decodeObjectForKey:@"fansCount"];
        self.goodCount = [decoder decodeObjectForKey:@"goodCount"];
        self.sex = [decoder decodeObjectForKey:@"sex"];
        self.login = [decoder decodeBoolForKey:@"login"];
        self.isCircle = [decoder decodeBoolForKey:@"isCircle"];
        self.cityName = [decoder decodeObjectForKey:@"cityName"];
        self.session_id = [decoder decodeObjectForKey:@"session_id"];
    }
    
    return  self;
}

- (void)cleanData
{
    self.userId = nil;
    self.userAlbum = nil;
    self.nickName = nil;
    self.signature = nil;
    self.birthday = nil;
    self.attentionCount = nil;
    self.fansCount = nil;
    self.goodCount = nil;
    self.sex = nil;
    self.login = NO;
    self.cityName = nil;
    self.session_id = nil;
}

- (BOOL)login
{
    if (self.userId.length > 0) {
        return YES;
    }
    else
    {
        return NO;
    }
}
@end


